package com.mdc.mansion.web.modules.system.web;

import java.util.HashMap;
import java.util.Map;

import com.mdc.mansion.core.modules.system.SysLogService;
import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.modules.system.entity.Log;
import com.mdc.mansion.web.common.web.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


/**
 * 日志Controller
 * @author chenmingdong
 * @version 2016-03-07
 */
@Controller
@RequestMapping(value = "${adminPath}/system/log")
public class LogController extends BaseController {

	@Autowired
	private SysLogService sysLogService;
	
	@ModelAttribute
	public Log get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return sysLogService.get(id);
		}else{
			return new Log();
		}
	}
	
	@RequiresPermissions("system:log:view")
	@RequestMapping(value = {"list", ""})
	public ModelAndView list(Log log) {
		Map<String, Object> model=new HashMap<String, Object>();
		Page<Log> page = sysLogService.findPage(new Page<Log>(),log);
        model.put("page", page);
		ModelAndView modelAndView=new ModelAndView("modules/system/logList", model);
		return modelAndView;
	}
	
	
	@RequiresPermissions("system:log:edit")
	@RequestMapping(value = "delete")
	public ModelAndView delete(Log log,RedirectAttributes redirectAttributes) {
		sysLogService.delete(log);
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
}
